package diagr.com;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

	public class GraphicsView extends View
		{
			private int heightC,x = 40,y = 10,maxX,maxY,Ykon,XX,max=0,width = 50,kol, col=0;
			private ShapeDrawable[] mDrawable;
			private int[] colors = new int[]{0xff40E0D0,0xff0000FF,0xff8A2BE2,0xffA52A2A,0xffFF1493,0xffFFD700,0xff7CFC00,0xffFFA500,0xffB0E0E6,0xffffEff0};
			int[] znachScaled;
			private Paint PaintOs;
			private Paint PaintText;
			Stolbik[] Stolb;
			Context cont;
			
		public GraphicsView(Context context,int w,int h,Stolbik[] mass,Settings sett) 
		{
				super(context);
		    	  	cont=context;
		    	  	Stolb=mass;
		    	  	int[] znach = new int[mass.length];
		    	  	for(int i=0;i<mass.length;i++)
		    		  znach[i]= mass[i].getHeight();
		    	  	
			        kol=znach.length;
			        maxX=w;
			        maxY=h;
			        heightC=h*5/8;
			        Ykon=heightC+y;
			        width=(maxX-80)/(kol);
			        
		    	 	PaintOs = new Paint();
			        PaintOs.setColor(sett.getOsColor());
			        PaintOs.setStrokeWidth(sett.getLineSize());
			        PaintOs.setStyle(Style.STROKE);
			        
			        PaintText = new Paint();
			        PaintText.setColor(sett.getTextColor());
			        PaintText.setTextSize(sett.getTextSize());
			        PaintText.setStrokeWidth(1);
			        PaintText.setStyle(Style.FILL);
			        PaintText.setTextAlign(Paint.Align.CENTER);
		
					mDrawable = new ShapeDrawable[kol];
					
					float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
					znachScaled= scaleTo(znach,heightC/100*33);
					
					XX =x + kol*(width+2) +2;
				for(int i =0; i<znachScaled.length;i++)
				{
					  mDrawable[i] = new ShapeDrawable(new RoundRectShape(outerR, null, null));
				      mDrawable[i].getPaint().setColor(Stolb[i].getColor());
				      mDrawable[i].setBounds(x+2 +(width+2)*i, y+heightC-3*znachScaled[i], x+2 +(width+2)*i +width, y + heightC );
				}
		}
			public int getMax(int[] ii) 
				{
		    	    for (int i : ii) 
		    	    {
		    	      if (max<i) max = i;
		    	    }
		    	    return max;
		    	  }
		    	  
		    	  private int[] scaleTo(int[] ii, int vmax) 
		    	  {
		    	    int[] scaled = new int[ii.length];
		    	    double k = (double) vmax / getMax(ii);
		    	    for(int i=0; i<ii.length; i++) 
		    	    {
		    	      scaled[i] = (int) (ii[i]*k);
		    	    }
		    	    return scaled;
		    	  }
		
		          public boolean onTouchEvent(MotionEvent event)
		          {
		        	switch (event.getAction()) 
		  		 	{

		  		 	case MotionEvent.ACTION_DOWN:
		  		 		{
		  		 			if((event.getY()>Ykon) && (event.getY()<Ykon+45))
		  		 			{
			  		 		 for(int i =0; i<kol;i++)
				      			{
				    			  if ( (event.getX() > x+2 +(width+2)*i) && (event.getX() < x+2 +(width+2)*i +width) )
				                  {
					                	col++;
					                	if(col>=colors.length) col=0;
					            		mDrawable[i].getPaint().setColor(colors[col]);
					            		Stolb[i].setColor(colors[col]);
					            		invalidate();
				                  }
				            		
				      			}
		  		 			}
		  		 			if((event.getX() < x)&&(event.getY()>50) && (event.getY()<Ykon))
		  		 			{
		  		 				Stolb[0].incDel();
				      			if(Stolb[0].getDel()>15)Stolb[0].setDel(4);
				            	invalidate();
		  		 			}

		  		 			for(int i =0; i<kol;i++)
			      			{
			            		  int bb = 235; 
			                 
			                  if ( (event.getX() > x+2 +(width+2)*i) && (event.getX() < x+2 +(width+2)*i +width) && (event.getY()>50) && (event.getY()<Ykon))
			                  {
			                	if((event.getY()>Ykon-3*znachScaled[i]) && (event.getY()<Ykon))
			                	{
			                		if(maxX>maxY) bb = 395;
			                		
			                		Toast v = Toast.makeText(cont,  Integer.toString(Stolb[i].getHeight()), Toast.LENGTH_SHORT);
			                		v.setGravity(48 ,(int)event.getX()-bb,(int)event.getY()-40);
			                		v.show();
			                      return true;
			                	}
			                  }
			      			}
			            }
		  		 	}
		  		 	return false;
		         }

		     protected void onDraw(Canvas canvas) 
		      {
		    	  canvas.drawLine(x-2, y, x-2, Ykon, PaintOs);
		     	  canvas.drawLine(XX, Ykon, XX, y, PaintOs);
		    	  canvas.drawLine(x-2, y, XX, y, PaintOs);
		    
		    	  for(int i =0; i< Stolb[0].getDel();i++)
		    	  {
		    	  canvas.drawLine(x-2, Ykon-i*(Ykon -y)/Stolb[0].getDel(), XX, Ykon-i*(Ykon-y)/Stolb[0].getDel(), PaintOs);
		    	  canvas.drawText(String.valueOf(i*max/Stolb[0].getDel()), 20 , Ykon +10 - i*(Ykon -y)/Stolb[0].getDel(), PaintText);
		    	  		    	  }
		    	   canvas.drawText(String.valueOf(max), 20 , y+10, PaintText);
		    	   int k =0,z=0,b=0;
		    	  
		    	   if(maxX>maxY)
			    	 {
		    		   if(Stolb.length%2==0)
		    			   k =2*maxX/(Stolb.length);
		    		   else
		    			   k =2*maxX/(Stolb.length+1);
			    		 int v=0,n=1;
			    		 for(int i = 0;i<Stolb.length ;i++) 
			    		  {
			    			 
			    		  if(i==2*n)
			    			  {
			    			  n++;
			    			  v++;
			    			  z=i;
			    			  }
			    		  canvas.drawText(String.valueOf(i+1), x+width/2 +(width+2)*i , Ykon +30, PaintText);
			    		  canvas.drawText(String.valueOf(i+1), x/3+k*v ,Ykon +60+(i-z)*20  , PaintText);
			    		  canvas.drawText("-", x/3+15+k*v , Ykon+60+(i-z)*20 , PaintText);
			    		  canvas.drawText(Stolb[i].getName(), x+40+k*v ,Ykon +60+(i-z)*20 , PaintText);
			    		  mDrawable[i].draw(canvas);
			    		  }
			    	 }
			    	 else
			    	 {

		    	   for(int i = 0;i<Stolb.length;i++) 
		    		  {
		    		   if(i==Stolb.length/2)
		    			  {
		    			  z=i;
		    			  k =maxX/2;
		    			  b=1;
		    			  }

		    		   canvas.drawText(String.valueOf(i+1), x+width/2 +(width+2)*i , Ykon +30, PaintText);
			    		  canvas.drawText(String.valueOf(i+1), x+k*b ,Ykon +60+(i-z)*20  , PaintText);
			    		  canvas.drawText("-", x+15+k*b , Ykon+60+(i-z)*20 , PaintText);
			    		  canvas.drawText(Stolb[i].getName(), 3*x+k*b ,Ykon +60+(i-z)*20 , PaintText);

		    		   mDrawable[i].draw(canvas);
		    		  }
			    	 }

		    	 }
}
		    

		     

	

